package lifeOfAFish.scenegraph.test;


import lifeOfAFish.test.JoglTemplate;
import java.io.File;

import javax.media.opengl.*;
//import javax.media.opengl.GLAutoDrawable;

import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;
import lifeOfAFish.scenegraph.*;
import lifeOfAFish.scenegraph.geom.*;


public class test_transform extends JoglTemplate {
	
	// scenegraph nodes
	sgNode root = new sgNode();
	sgTransform trans = new sgTransform();
	sgTransform rot = new sgTransform();
	sgGeomCube cube = new sgGeomCube();
	
	sgTransform rot2 = new sgTransform();
	sgTransform rotOffset = new sgTransform();
	sgGeomCube cube2 = new sgGeomCube();
	sgTransform trans2 = new sgTransform();
	sgTransform scale2 = new sgTransform();
	
	float theta = 0.0f;
	float theta2 = 0.0f;
	float step = 1.0f;
	
	
	public static void main (String[] args){
		test_transform template = new test_transform();
		template.setVisible(true);
		
	}

	public void init(GLAutoDrawable drawable)
	{
		super.init(drawable);
		
		// get gl object
		GL gl = drawable.getGL();
		sgGlobals.gl = gl;
		
		// enable depth testing
		gl.glEnable(GL.GL_DEPTH_TEST);
		gl.glDepthRange(0.0, 1000.0);
		
		// enable lighting
		gl.glEnable(GL.GL_LIGHTING);
		gl.glEnable(GL.GL_LIGHT0);
		gl.glShadeModel(GL.GL_SMOOTH);

		
		float light_ambient[] = { 1.0f, 1.0f, 1.0f, 1.0f };
		float light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
		float light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
		float light_position[] = { 1.0f, 1.0f, -1.0f, 1.0f };

		gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, light_ambient, 0);
		gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, light_diffuse, 0);
		gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, light_specular, 0);
		gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position, 0);
		
		//gl.glEnable(GL.GL_COLOR_MATERIAL);
		
		// construct the scenegraph tree.
		root.addChild(rot);
		rot.addChild(trans);
		trans.addChild(cube);
		trans.translate(2.0f, -1.0f, 0.0f);
		
		rot.addChild(rotOffset);
		rotOffset.rotate(180.0f, 0.0f, 1.0f, 0.0f);
		rotOffset.addChild(trans2);
		trans2.translate(1.5f, 1.0f, 0.0f);
		trans2.addChild(rot2);
		rot2.addChild(scale2);
		scale2.scale(0.5f, 0.5f, 0.5f);
		scale2.addChild(cube2);
		
		sgMaterial mat1 = new sgMaterial();
		mat1.setAmbient(0.5f, 0.5f, 0.5f, 1.0f);
		mat1.setDiffuse(1.0f, 1.0f, 1.0f, 1.0f);
		mat1.setSpecular(1.0f, 1.0f, 1.0f, 1.0f);
		cube.setMaterial(mat1);
		System.out.println("mat1 : " + mat1);
		
		sgMaterial mat2 = new sgMaterial();
		mat2.setAmbient(0.1f, 0.1f, 0.1f, 1.0f);
		mat2.setDiffuse(0.5f, 0.0f, 0.0f, 1.0f);
		mat2.setSpecular(0.5f, 0.0f, 0.0f, 1.0f);
		mat2.setEmission(0.0f, 0.0f, 0.2f, 1.0f);
		cube2.setMaterial(mat2);
		System.out.println("mat2 : " + mat2);
	}
	
	
	/*
	 * overwritten version of display
	 */
	public void display(GLAutoDrawable drawable)
	{
		// get the gl object
		GL gl = drawable.getGL();
		// set the erasing color (black)
		gl.glClearColor(0f, 0f, 0f, 0f);
		// clear screen with the defined erasing color and depth buffer
		
		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
		gl.glPushMatrix();
		applyMouseTranslation(gl);
		applyMouseRotation(gl);
		
		rot.rotate(theta, 0.0f, 1.0f, 0.0f);
		theta += step;
		
		rot2.rotate(theta2, 0.0f, 1.0f, 0.0f);
		theta2 -= 2*step;
		
		
		// fire the traversal
		root.traverse();
		
		// delay for a while
//		double time = System.currentTimeMillis();
//		while (System.currentTimeMillis() - time < 80);
		
		
		// load an object using the mesh loader
		gl.glPopMatrix();
	}
	
	
}
